package mpbe.Componentes.TableColEdit;

/*
 * Este codigo es software libre y se encuentra licenciado 
 * bajo los términos de la Licencia Pública General de GNU versión 3 
 * según se encuentra publicada por la Free Software Foundation.
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Autor: Martin Pablo Bellanca <martin.bellanca@gmail.com>
 * web: http://mpbe.blogspot.com/
 */

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class WgTextCE extends WidgetsTCE {
	private Text wgEditor = null;
	private ModifyCellListener modifyListener = null;
	
	private EditingTCE editandoT;
	private TableCursor cursorT;
	private ControlEditor editorT;
	private Table tableT;
	
	public WgTextCE(int SWT, String[] lista, String txtdefault) {
		super(SWT, lista, txtdefault);
		//wgEditor = new Text(cursor, SWT);
	}

	public WgTextCE(int SWT) {
		super(SWT);
		super.TXTDefault = "";
		//wgEditor = new Text(cursor, SWT);
	}

	public WgTextCE(int SWT, String[] lista) {
		super(SWT, lista);
		super.TXTDefault = "";
		//wgEditor = new Text(cursor, SWT);
	}

	public WgTextCE(TableCursor cursor, int SWT, String txtdefault) {
		super(SWT, txtdefault);
		//wgEditor = new Text(cursor, SWT);
	}

	@Override
	public Control getWidget() {
		return wgEditor;
	}

	@Override
	public int indexOfList(String TXT) {
		int ii = -1, jj=0;
		do {
			if (super.TXTList[jj]==TXT) ii=jj;
			jj++;
		} while ((ii<0) && (jj<super.TXTList.length));
		return ii;
	}

	@Override
	public void onCursoKeyChar(EditingTCE editando, TableCursor cursor, ControlEditor editor, Table table, char car) {
		//System.out.println("cursor key caracter");
		if (cursor.getRow() != null) {
			editando.startEditing(cursor.getRow(), cursor.getColumn());
			editando.setText(""+car);
			editandoT = editando;
			cursorT = cursor;
			editorT = editor;
			tableT = table;
			editar(false);
		}
	}

	@Override
	public void onCursorKeyF2(EditingTCE editando, TableCursor cursor, ControlEditor editor, Table table) {
		//System.out.println("TextCE: cursor key F2");
		//TableCursor cursor = tableCE.getCursor();
		//EditingTCE editando = tableCE.getEditing();
		if (cursor.getRow() != null) {
			editando.startEditing(cursor.getRow(), cursor.getColumn());
			editandoT = editando;
			cursorT = cursor;
			editorT = editor;
			tableT = table;
			editar(true);
		}
	}

	@Override
	public void onCursorMouseDown(EditingTCE editando, TableCursor cursor, ControlEditor editor, Table table) {
		// System.out.println("cursor mouse down ");
		if (cursor.getRow() != null) {
			editandoT = editando;
			cursorT = cursor;
			editorT = editor;
			tableT = table;
			editando.startEditing(cursor.getRow(), cursor.getColumn());
			editar(true);
		}
	}

	@Override
	public void onTableMouseDown(EditingTCE editando) {
		String txt = wgEditor.getText();
		editando.setText(txt);
		if (modifyListener!=null) modifyListener.modifyCell(tableT.getSelectionIndex(), (Object) wgEditor.getText());
		editando.endEditing();
		//wgEditor = null; //
		wgEditor.dispose();
	}
	
	private void editar(boolean selecttxt){
		//System.out.println("editanto TXT");
		//table.setSelection(edtRow);
		wgEditor = new Text(cursorT, wgSWT);
		wgEditor.setText(editandoT.getText());
		if (selecttxt == true) 
			wgEditor.selectAll(); 
		else 
			wgEditor.setSelection(wgEditor.getCharCount());
		wgEditor.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				//table.setSelection(cursor.getRow());
				TableItem row = editandoT.getRow();
				int column = editandoT.getColumn();
				int rowi = tableT.getSelectionIndex();
				//	 close the text editor and copy the data over
				//	 when the user hits "ENTER"
				if (e.character == SWT.CR) {
					row.setText(column, wgEditor.getText());
					if (modifyListener!=null) modifyListener.modifyCell(rowi, (Object) wgEditor.getText());
					wgEditor.dispose();
					editandoT.endEditing();
					cursorT.setFocus();
				}
				//	close the text editor when the user hits "ESC"
				else if (e.character == SWT.ESC) {
					wgEditor.dispose();
					editandoT.endEditing();
					cursorT.setFocus();
				}
				else if(e.keyCode==SWT.ARROW_LEFT && (e.stateMask & SWT.CONTROL) != 0){
					row.setText(column, wgEditor.getText());
					if (modifyListener!=null) modifyListener.modifyCell(rowi, (Object) wgEditor.getText());
					if (column>1) cursorT.setSelection(rowi, column-1);
					wgEditor.dispose();
					editandoT.endEditing();
					cursorT.setFocus();
				}
				else if(e.keyCode==SWT.ARROW_RIGHT && (e.stateMask & SWT.CONTROL) != 0){
					row.setText(column, wgEditor.getText());
					if (modifyListener!=null) modifyListener.modifyCell(rowi, (Object) wgEditor.getText());
					if (column<tableT.getColumnCount()-1) cursorT.setSelection(rowi, column+1);
					wgEditor.dispose();
					editandoT.endEditing();
					cursorT.setFocus();
				}
				else if(e.keyCode==SWT.ARROW_DOWN){
					row.setText(column, wgEditor.getText());
					if (modifyListener!=null) modifyListener.modifyCell(rowi, (Object) wgEditor.getText());
					if (rowi<tableT.getItemCount()-1){
						cursorT.setSelection(rowi+1,column);
						tableT.setSelection(cursorT.getRow());
					}
					wgEditor.dispose();
					editandoT.endEditing();
					cursorT.setFocus();
				}
				else if(e.keyCode==SWT.ARROW_UP){
					row.setText(column, wgEditor.getText());
					if (modifyListener!=null) modifyListener.modifyCell(rowi, (Object) wgEditor.getText());
					if (rowi>1){
						cursorT.setSelection(rowi-1, column);
						tableT.setSelection(cursorT.getRow());
					}
					wgEditor.dispose();
					editandoT.endEditing();
					cursorT.setFocus();
				}
				//cursorT.setFocus();
			}
		});
		editorT.setEditor(wgEditor);
		wgEditor.setFocus();		
	}
	
	// funciones de listener
	public void addModifyCellListener(ModifyCellListener listener) {
		modifyListener = listener;
	}
	
	public void removeModifyCellListener() {
		modifyListener = null;
	}
}
